package com.icangya.DAO.impl;

import com.icangya.DAO.IRedPacketRecordDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.RedPacketRecord;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.icangya.Value.FootBall.FOOT_PAGE_ROW;


@Repository
public class RedPacketRecordDAOImpl extends  BaseDAO<RedPacketRecord> implements IRedPacketRecordDAO {

    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public List getRedPacketRecord(long uid) {
        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        String hql=" FROM RedPacketRecord pr where pr.uid='"+uid+"' order by pr.createTime desc";
        Query query=session.createQuery(hql);
        query.setMaxResults(14);
        List list=query.list();
        if(list !=null)
            return  list;
        return null;
    }

    @Override
    public List<Object> findRedPacketRecordByUserId(Long uid, Integer pageNum) {
        String hql="FROM RedPacketRecord er where er.uid='"+uid+"' order by er.createTime desc";
        String count = "SELECT COUNT(er) " + hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, pageNum, FOOT_PAGE_ROW);
    }
}
